package com.lsh.config;

import com.lsh.datasource.DynamicDataSourceContextHolder;
import com.lsh.enu.DataSourceType;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * @author ：LiuShihao
 * @date ：Created in 2021/5/20 9:38 上午
 * @desc ：定时任务配置
 */
@Configuration
public class ScheduleConfig {
    private static ConfigurableListableBeanFactory beanFactory;


    public ScheduleConfig(){
        System.out.println("加载ScheduleConfig定时任务配置...");
    }
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(@Qualifier("slaveDataSource")DataSource dataSource) {
        /**
         * 手动切换数据源
         */
        System.out.println("注入SchedulerFactoryBean...");
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
        System.out.println(DynamicDataSourceContextHolder.getDataSourceType());
        //设置数据源
        factory.setDataSource(dataSource);
        // quartz参数
        Properties prop = new Properties();
        //线程调度器实例名
        prop.put("org.quartz.scheduler.instanceName", "RuoyiScheduler");
        //实例ID，设为AUTO则由Quartz自动根据主机名、时间戳生成实例ID
        prop.put("org.quartz.scheduler.instanceId", "AUTO");
        // 线程池配置
        prop.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
        //线程池的线程数，即最多20个任务同时跑
        prop.put("org.quartz.threadPool.threadCount", "20");
        prop.put("org.quartz.threadPool.threadPriority", "5");
        // JobStore配置 如何存储任务和触发器等信息   存储任务为数据库存储，不能使用内存，即RAMJobStore
        prop.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
        // 集群配置 是否集群
        prop.put("org.quartz.jobStore.isClustered", "true");
        prop.put("org.quartz.jobStore.clusterCheckinInterval", "15000");
        prop.put("org.quartz.jobStore.maxMisfiresToHandleAtATime", "1");
        prop.put("org.quartz.jobStore.txIsolationLevelSerializable", "true");

        // sqlserver 启用
        // prop.put("org.quartz.jobStore.selectWithLockSQL", "SELECT * FROM {0}LOCKS UPDLOCK WHERE LOCK_NAME = ?");
        prop.put("org.quartz.jobStore.misfireThreshold", "12000");
        //表前缀
        prop.put("org.quartz.jobStore.tablePrefix", "QRTZ_");
        factory.setQuartzProperties(prop);

        factory.setSchedulerName("RuoyiScheduler");
        // 延时启动
        factory.setStartupDelay(1);
        factory.setApplicationContextSchedulerContextKey("applicationContextKey");
        // 可选，QuartzScheduler
        // 启动时更新己存在的Job，这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了  覆盖已存在的任务
        factory.setOverwriteExistingJobs(true);
        // 设置自动启动，默认为true
        factory.setAutoStartup(true);

        return factory;
    }
}

